package huawei.js2025;

import jdk.nashorn.internal.parser.JSONParser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * 处理器问题
 */
public class Question15 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String string = sc.nextLine();
        int jobCount = sc.nextInt();
        sc.close();
        List<Integer> cpuFree = Arrays.stream(string.trim().substring(1, string.length() - 1).split(",")).map(Integer::parseInt).collect(Collectors.toList());

        int left = 0;
        for (int i = 0; i < 4; i++) {
            if (cpuFree.contains(i)) {
                left += 1;
            }
        }

        int right = 0;
        for (int i = 4; i < 8; i++) {
            if (cpuFree.contains(i)) {
                right += 1;
            }
        }

        boolean useLeft = false;
        boolean useRight = false;

        List<List<Integer>> result = new ArrayList<>();
        switch (jobCount) {
            case 1: {
                // 一个最佳
                if (left == 1) {
                    useLeft = true;
                    useRight = right == 1;
                } else if (right == 1) {
                    useRight = true;
                } else if (left == 3) {
                    useLeft = true;
                    useRight = right == 3;
                } else if (right == 3) {
                    useRight = true;
                } else if (left == 2) {
                    useLeft = true;
                    useRight = right == 2;
                } else if (right == 2) {
                    useRight = true;
                } else if (left == 4) {
                    useLeft = true;
                    useRight = right == 4;
                } else if (right == 4) {
                    useRight = true;
                }

                if (useLeft) {
                    select(cpuFree, result, new ArrayList<>(), true, 1, 0);
                }

                if (useRight) {
                    select(cpuFree, result, new ArrayList<>(), false, 2, 4);
                }

                break;
            }
            case 2: {
                if (left == 2) {
                    useLeft = true;
                    useRight = right == 2;
                } else if (right == 2) {
                    useRight = true;
                } else if (left == 4) {
                    useLeft = true;
                    useRight = right == 4;
                } else if (right == 4) {
                    useRight = true;
                } else if (left == 3) {
                    useLeft = true;
                    useRight = right == 3;
                } else if (right == 3) {
                    useRight = true;
                }
                if (useLeft) {
                    select(cpuFree, result, new ArrayList<>(), true, 2, 0);
                }
                if (useRight) {
                    select(cpuFree, result, new ArrayList<>(), false, 2, 4);
                }
                break;
            }
            case 4: {
                if (left == 4) {
                    useLeft = true;
                }
                if (right == 4) {
                    useRight = true;
                }
                if (useLeft) {
                    select(cpuFree, result, new ArrayList<>(), true, 4, 0);
                }
                if (useRight) {
                    select(cpuFree, result, new ArrayList<>(), false, 4, 4);
                }

                break;
            }
            case 8: {
                if (left == 4 && right == 4) {
                    List<Integer> list = new ArrayList<>();
                    list.add(1);
                    list.add(2);
                    list.add(3);
                    list.add(4);
                    list.add(5);
                    list.add(6);
                    list.add(7);
                    list.add(8);
                    result.add(list);
                }
            }

        }

        System.out.println(result.toString().replaceAll(" ", ""));
    }

    public static void select(List<Integer> cpuFree,List<List<Integer>> result, List<Integer> list, boolean isLeft, int count, int i) {
        if (list.size() == count) {
            result.add(new ArrayList<>(list));
            return;
        }
        if ((isLeft && i >= 4) || (!isLeft && i >= 8)) {
            return;
        }
        int border = isLeft ? 4 : 8;

        for (int j = i; j < border; j++) {
            if (cpuFree.contains(j)) {
                list.add(j);
                select(cpuFree, result, list, isLeft, count, j + 1);
                list.remove(list.size() - 1);
            }
        }

    }
}
